package cn.jit.ssm.web.controller.support;

import cn.jit.ssm.constant.SessionKey;
import cn.jit.ssm.service.AccountService;
import cn.jit.ssm.support.JSONReturn;
import cn.jit.ssm.utils.RandomValidateCodeUtil;
import cn.jit.ssm.utils.CompareUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: Cecurio
 * @create: 2017-12-07 17:19
 **/
@Controller
public class SupportController extends BaseController {

    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "0/findVerifyCode",method = RequestMethod.GET)
    public void findVerifyCode(HttpServletRequest request, HttpServletResponse response) {
        RandomValidateCodeUtil.getRandomcode(request, response);
    }

    @ResponseBody
    @RequestMapping(value = "0/acctLogin",method = RequestMethod.POST)
    public JSONReturn login(@RequestParam String name, @RequestParam String pass, @RequestParam String verify,
                            HttpServletRequest request) {
        if (CompareUtil.isEmpty(verify)) {
            return JSONReturn.buildFailure("登录失败, 验证码必填!");
        }

        String sessionVerify = (String) request.getSession().getAttribute(SessionKey.VALIDATE_CODE);
        if (StringUtils.isEmpty(sessionVerify) || !verify.equalsIgnoreCase(sessionVerify)) {
            return JSONReturn.buildFailure("登录失败, 验证码出错!");
        }

        return accountService.login(name, pass, request);
    }
}
